//
//  SongListViewController.m
//  YanMusicPlayer
//
//  Created by 1564335 on 15-6-17.
//  Copyright (c) 2015年 胡岩 All rights reserved.
//

#import "SongListViewController.h"
#import "collecAndDownloadTableViewCell.h"
#import "RankMusicList.h"
@interface SongListViewController ()<UITableViewDataSource, UITableViewDelegate, HeaderViewSize, sendSingerTouch, UIAlertViewDelegate>

@property(nonatomic, strong) UITableView *mainTV;

@property(nonatomic, strong) NSMutableArray *isOpen;

@property(nonatomic, assign) BOOL isDelect;

@property(nonatomic, strong) UIView *lineView;

@property(nonatomic, assign) CGFloat currentX;

@property(nonatomic, assign) CGFloat currentY;

@property(nonatomic, assign) CGFloat totalDistance;

@property(nonatomic, strong) HearderView *hearderView;

@property(nonatomic, assign) CGFloat startY;

@property(nonatomic, strong) UIView *backgroundView;

@property(nonatomic, assign) NSInteger theTime;

@property(nonatomic, strong) UIButton *addTableBtn;

@property(nonatomic, strong) UIButton *deleteBtn;

@property(nonatomic, assign) BOOL isDeleting;

@property(nonatomic, strong) UIImageView *upImageView;

@property(nonatomic, assign) NSInteger currentSection;


@end

@implementation SongListViewController



- (void)sendBegin
{
    [[CDSideBarController sharedInstance] dismissMenu];
    _startY = _hearderView.frame.origin.y;
}
- (void)sendHeaderViewSize:(CGFloat)height
{
    _totalDistance += height;
    _currentY = height;
    if ((_hearderView.frame.origin.y + height) < _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT) && _hearderView.frame.origin.y + height > 74 * (_HEIGHT / _DEFAULT_HEIGHT)) {
        _hearderView.center = CGPointMake(_hearderView.center.x, _hearderView.center.y + height);
        if (_totalDistance > 0 && _startY != _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)) {
            [HearderView shareInstance].isList = NO;
            [UIView animateWithDuration:0.8 animations:^{
                [HearderView shareInstance].listTableView.alpha = 0.0f;
                [HearderView shareInstance].titleImageView.alpha = 1.0f;
            }];
            
            _backgroundView.alpha = 0.7 - 0.7 * (_totalDistance / (_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)));
        } else if (_totalDistance > -(_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)) && _startY != 74 * (_HEIGHT / _DEFAULT_HEIGHT)) {
            _backgroundView.alpha = 0.7 * (-_totalDistance / (_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)));
        }
    }
    [self.view bringSubviewToFront:_hearderView];
}

- (void)sendOver
{
    if (_theTime == 0 && _startY == (_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)) && _totalDistance < 0) {
        _backgroundView.userInteractionEnabled = NO;
        _theTime = 1;
    } else if (_theTime == 1 && _startY == (74 * (_HEIGHT / _DEFAULT_HEIGHT)) && _totalDistance > 0) {
        _backgroundView.userInteractionEnabled = YES;
        _theTime = 0;
    }
    
    if (_currentY > 0) {
        [UIView animateWithDuration:0.8 animations:^{
            _hearderView.frame = CGRectMake(0, _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT);
            _backgroundView.alpha = 0.0;
        }];
    } else if (_currentY <= 0) {
        [UIView animateWithDuration:0.8 animations:^{
            _hearderView.frame = CGRectMake(0, 74 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT);
            _backgroundView.alpha = 0.7;
        }];
    }
    [self.view bringSubviewToFront:_hearderView];
    _totalDistance = 0;
    _currentY = 0;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_isOpen removeAllObjects];
    for (int i = 0; i < [MusicPlayer1 shareInstance].mainList.count + 1; i++) {
        NSString *isOpen = @"NO";
        [_isOpen addObject:isOpen];
    }
    _isDelect = NO;
    [HearderView shareInstance].isSongListView = YES;
    [HearderView shareInstance].title.alpha = 0.0;
    _backgroundView.alpha = 0.0;
    _hearderView = [HearderView shareInstance];
    _hearderView.frame = CGRectMake(0, _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT);
    
    [self.view addSubview:_hearderView];
    _hearderView.HeaderViewDelegate = self;
    [self.view bringSubviewToFront:_hearderView];
    [HearderView shareInstance].titleImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
    [HearderView shareInstance].titleImageView.center = CGPointMake(37 * (_WIDTH / _DEFAULT_WIDTH), 32 * (_HEIGHT / _DEFAULT_HEIGHT));
    
    [HearderView shareInstance].lastButton.frame = CGRectMake(_WIDTH - 240 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    [HearderView shareInstance].startButton.frame = CGRectMake(_WIDTH - 160 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    [HearderView shareInstance].nextButton.frame = CGRectMake(_WIDTH - 80 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    
    [HearderView shareInstance].progressSlider.alpha = 0;
    [HearderView shareInstance].likeBtn.alpha = 0;
    [HearderView shareInstance].downloadBtn.alpha = 0;
    [_mainTV reloadData];
    
}

- (void)tap
{
    [[CDSideBarController sharedInstance] dismissMenu];
    [UIView animateWithDuration:0.8 animations:^{
        _backgroundView.alpha = 0.0;
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:@"notification" object:nil];
    
    _currentSection = 1000;
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _WIDTH, _HEIGHT)];
    [self.view addSubview:backgroundImageView];
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *backgroundView = [[UIVisualEffectView alloc] initWithEffect:effect];
    backgroundView.frame = CGRectMake(0, 0, _WIDTH, _HEIGHT);
    [self.view addSubview:backgroundView];
    [backgroundImageView setImage:[UIImage imageNamed:@"bgImage2"]];
    
    
    RankMusicList *rank = [[RankMusicList alloc] init];
    rank.touchTableViewDelegate = self;
    
    // 初始化背景
    _backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    _backgroundView.backgroundColor = [UIColor blackColor];
    _backgroundView.alpha = 0.0;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [_backgroundView addGestureRecognizer:tap];
    [self.view addSubview:_backgroundView];
    
    _hearderView = [HearderView shareInstance];
    _hearderView.frame = CGRectMake(0, _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT);
    [self.view addSubview:_hearderView];
    _hearderView.HeaderViewDelegate = self;
    
    _mainTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 90 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH, _HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) style:UITableViewStylePlain];
    [self.view addSubview:_mainTV];
    _mainTV.delegate = self;
    _mainTV.dataSource = self;
    _mainTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _isOpen = [NSMutableArray array];
    _mainTV.backgroundColor = [UIColor clearColor];
    _mainTV.showsVerticalScrollIndicator = NO;
    
    _addTableBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _addTableBtn.frame = CGRectMake(10, 30 * (_HEIGHT / _DEFAULT_HEIGHT), 50 * (_HEIGHT / _DEFAULT_HEIGHT), 50 * (_HEIGHT / _DEFAULT_HEIGHT));
    [self.view addSubview:_addTableBtn];
    [_addTableBtn addTarget:self action:@selector(addTable) forControlEvents:UIControlEventTouchUpInside];
//    _addTableBtn.backgroundColor = [UIColor redColor];
    [_addTableBtn setImage:[UIImage imageNamed:@"iconfont-tianjia"] forState:UIControlStateNormal];
    
    _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteBtn.frame = CGRectMake(70, 30 * (_HEIGHT / _DEFAULT_HEIGHT), 50 * (_HEIGHT / _DEFAULT_HEIGHT), 50 * (_HEIGHT / _DEFAULT_HEIGHT));
    [self.view addSubview:_deleteBtn];
    [_deleteBtn addTarget:self action:@selector(dltTable:) forControlEvents:UIControlEventTouchUpInside];
    [_deleteBtn setImage:[UIImage imageNamed:@"iconfont-icon52"] forState:UIControlStateNormal];
    
    [self.view bringSubviewToFront:_backgroundView];
    [self.view bringSubviewToFront:_hearderView];
}

- (void)dltTable:(UIButton *)button
{
    [[CDSideBarController sharedInstance] dismissMenu];
    if (_isDelect == NO) {
        [_isOpen removeAllObjects];
        for (int i = 0; i < [MusicPlayer1 shareInstance].mainList.count + 1; i++) {
            NSString *isOpen = @"NO";
            [_isOpen addObject:isOpen];
        }
        _isDelect = YES;
        [_mainTV reloadData];
    } else {
        _isDelect = NO;
        [_mainTV reloadData];
    }
    
}

- (void)addTable
{
    [[CDSideBarController sharedInstance] dismissMenu];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入歌单名称" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *text = [alertView textFieldAtIndex:0].text;
    if (buttonIndex == 0 && text.length != 0) {
        NSInteger str = [[_USERDEFAULT objectForKey:@"tableCount"] integerValue];
        [Database insertTableName:[NSString stringWithFormat:@"%ld", str] :text];
        NSString *getStr = [NSString stringWithFormat:@"%ld", ++str];
        [_USERDEFAULT setObject:getStr forKey:@"tableCount"];
        NSMutableArray *array = [NSMutableArray array];
        [[MusicPlayer1 shareInstance].mainList addObject:array];
        NSString *isOpen = @"NO";
        [_isOpen addObject:isOpen];
        [Database createTable];
        [_mainTV reloadData];
    }
}


- (void)deleteButton:(UIButton *)button
{
    [[CDSideBarController sharedInstance] dismissMenu];
    NameModel *model = [[Database selectTableName] objectAtIndex:button.tag];
    [_isOpen removeObjectAtIndex:button.tag + 1];
    [Database deleteTable:model.tableName];
    [Database deleteTableName:model.tableName];
    [[MusicPlayer1 shareInstance].mainList removeObjectAtIndex:button.tag];
    [_isOpen removeAllObjects];
    for (int i = 0; i < [MusicPlayer1 shareInstance].mainList.count + 1; i++) {
        NSString *isOpen = @"NO";
        [_isOpen addObject:isOpen];
    }
    _isDelect = NO;
    [_mainTV reloadData];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    collecAndDownloadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[collecAndDownloadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSArray *arrayCollect = [Database selectData:@""];
    [[MusicPlayer1 shareInstance].collectList removeAllObjects];
    [[MusicPlayer1 shareInstance].downloadList removeAllObjects];
    [[MusicPlayer1 shareInstance].collectList addObjectsFromArray:arrayCollect];
    NSArray *arrayDownload = [Database selectDownloadData];
    [[MusicPlayer1 shareInstance].downloadList addObjectsFromArray:arrayDownload];
    
        MusicModel *model = nil;
    if (indexPath.section == 0) {
        model = [[MusicPlayer1 shareInstance].collectList objectAtIndex:indexPath.row];
        [cell setMusicModel:model];
    }
//    else if (indexPath.section == 1) {
//        model = [[MusicPlayer1 shareInstance].downloadList objectAtIndex:indexPath.row];
//        [cell setMusicModel:model];
//    }
    else {
        NSArray *array = [[MusicPlayer1 shareInstance].mainList objectAtIndex:indexPath.section - 1];
        model = [array objectAtIndex:indexPath.row];
        [cell setMusicModel:model];
    }
    
    NSArray *array = [Database selectData:@""];
    BOOL isOK = YES;
    for (MusicModel *dbModel in array) {
        if ([dbModel.songId integerValue] == [model.songId integerValue]) {
//            if (indexPath.section == 1) {
//                cell.collectBtn.userInteractionEnabled = NO;
//                cell.collectImage = @"iconfont-xiazaiwancheng";
//            } else {
                cell.collectImage = @"iconfont-xihuan";
                cell.collectBtn.userInteractionEnabled = YES;
                isOK = NO;
                break;
//            }
        }
    }
    if (isOK) {
//        if (indexPath.section == 1) {
//            cell.collectBtn.userInteractionEnabled = NO;
//            cell.collectImage = @"iconfont-xiazaiwancheng";
//        } else {
            cell.collectBtn.userInteractionEnabled = YES;
            cell.collectImage = @"iconfont-xihuan3";
//        }
    }

    
    [cell.collectBtn addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
    NSString *isOpen = [_isOpen objectAtIndex:indexPath.section];
    if ([isOpen isEqualToString:@"YES"]) {
//        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 49, _WIDTH, 1)];
//        _lineView.tag = indexPath.section + 1;
//        _lineView.backgroundColor = [UIColor grayColor];
//        [cell addSubview:_lineView];
        cell.collectBtn.tag =  indexPath.section * 1000 + indexPath.row;
    }
    if (indexPath.row % 2 == 0) {
        cell.contentView.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    } else {
        cell.contentView.backgroundColor = [UIColor whiteColor];
    }
    return cell;
}

- (void)reloadSong
{
    [_mainTV reloadData];
}

//- (void)getNotification:(NSNotification *)notificttion
//{
//    [_mainTV reloadData];
//}

#pragma -mark 收藏
- (void)collect:(UIButton *)button
{
    MusicModel *model = nil;
    if (button.tag / 1000 == 0) {
        model = [[MusicPlayer1 shareInstance].collectList objectAtIndex:button.tag % 1000];
    }
//    else if (button.tag / 1000 == 1) {
//        //        model = [[MusicPlayer1 shareInstance].downloadList objectAtIndex:button.tag % 1000];
//    }
    else {
        NSArray *array = [[MusicPlayer1 shareInstance].mainList objectAtIndex:button.tag / 1000 - 1];
        model = [array objectAtIndex:button.tag % 1000];
    }
    NSArray *array = [Database selectData:@""];
    BOOL isOK = YES;
    for (MusicModel *dbModel in array) {
        if ([dbModel.songId integerValue] == [model.songId integerValue]) {
            isOK = NO;
            break;
        }
    }
    if (isOK) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag % 1000 inSection:button.tag / 1000];
        collecAndDownloadTableViewCell *cell = [_mainTV cellForRowAtIndexPath:indexPath];
        
        cell.collectImage = @"iconfont-xihuan";
        [_mainTV reloadData];
        [[MusicPlayer1 shareInstance].collectList addObject:model];
        [Database insertValue:model];
        [[MusicPlayer1 shareInstance].list removeAllObjects];
        [[MusicPlayer1 shareInstance].list addObjectsFromArray:[MusicPlayer1 shareInstance].collectList];
        [[HearderView shareInstance].listTableView reloadData];
    } else {
        //                if (_currentSection != 0 || (_currentSection == 0 && [MusicPlayer1 shareInstance].list.count > 1)) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag % 1000 inSection:button.tag / 1000];
        collecAndDownloadTableViewCell *cell = [_mainTV cellForRowAtIndexPath:indexPath];
        
        cell.collectImage = @"iconfont-xihuan3";
        [Database deleteValue:model.songId];
        NSArray *array = [Database selectData:@""];
        [[MusicPlayer1 shareInstance].collectList removeAllObjects];
        [[MusicPlayer1 shareInstance].collectList addObjectsFromArray:array];
        [_mainTV reloadData];
        if ((button.tag % 1000) <= [MusicPlayer1 shareInstance].currentMusic && [MusicPlayer1 shareInstance].currentMusic != 0) {
            [MusicPlayer1 shareInstance].currentMusic--;
        }
        //                }
        
        
    }
//        if (_currentSection == 0 && [MusicPlayer1 shareInstance].list.count > 1) {
//            [[MusicPlayer1 shareInstance].list removeAllObjects];
//            [[MusicPlayer1 shareInstance].list addObjectsFromArray:[MusicPlayer1 shareInstance].collectList];
//            [[HearderView shareInstance].listTableView reloadData];
//        }
    [[HearderView shareInstance] reloadInputViews];
    [_mainTV reloadData];
}


- (void)showList:(UITapGestureRecognizer *)showTap
{
    UIView *tapView = showTap.view;
    for (UIView *view in _mainTV.subviews) {
        if (view.tag == tapView.tag + 1) {
            [view removeFromSuperview];
        }
    }
    for (int i = 0; i < _isOpen.count; i++) {
        NSString *getOpen = [_isOpen objectAtIndex:i];
        if (tapView.tag == i && [getOpen isEqualToString:@"NO"]) {
            NSString *isOpen = @"YES";
            [_isOpen replaceObjectAtIndex:i withObject:isOpen];
        } else if (tapView.tag == i && [getOpen isEqualToString:@"YES"]) {
            NSString *isOpen = @"NO";
            [_isOpen replaceObjectAtIndex:i withObject:isOpen];
        }
    }
    
    [_mainTV reloadData];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *backgroundView = [[UIVisualEffectView alloc] initWithEffect:effect];
    backgroundView.frame = CGRectMake(0, 0, _WIDTH, 50);
    
    UIImageView *titleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _WIDTH, 50)];
    [titleView setImage:[UIImage imageNamed:@"biaotilan"]];
    [titleView addSubview:backgroundView];
    titleView.userInteractionEnabled = YES;
    titleView.layer.borderColor = [UIColor orangeColor].CGColor;
    titleView.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:150 / 255.0 alpha:1];
    titleView.layer.borderWidth = 0.5;
    titleView.tag = section;
    UITapGestureRecognizer *showTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showList:)];
    [titleView addGestureRecognizer:showTap];
    _upImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_WIDTH - 50, 10, 30, 30)];
    NSString *getOpen = [_isOpen objectAtIndex:section];
    if ([getOpen isEqualToString:@"NO"] && _isDelect == NO) {
        [titleView addSubview:_upImageView];
        [_upImageView setImage:[UIImage imageNamed:@"iconfont-zhankai"]];
    } else if ([getOpen isEqualToString:@"YES"] && _isDelect == NO) {
        [titleView addSubview:_upImageView];
        [_upImageView setImage:[UIImage imageNamed:@"iconfont-guanbi"]];
    }
    
    if (_isDelect && section > 0) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(titleView.frame.size.width - 60, 0, 50, 50);
        [titleView addSubview:button];
//        button.backgroundColor = [UIColor redColor];
        [button setImage:[UIImage imageNamed:@"iconfont-icon52"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(deleteButton:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = section - 1;
    }
    
    if (section == 0) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleView.frame.size.width - 60, titleView.frame.size.height)];
        label.text = @"  喜欢列表";
        [titleView addSubview:label];
    }
//    else if (section == 1) {
//        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleView.frame.size.width - 60, titleView.frame.size.height)];
//        label.text = @"  下载列表";
//        [titleView addSubview:label];
//    }
    else {
        NSArray *array = [Database selectTableName];
        if (array.count > 0) {
            NameModel *model = [array objectAtIndex:section - 1];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleView.frame.size.width - 60, titleView.frame.size.height)];
            label.text = [NSString stringWithFormat:@"  %@", model.name];
            [titleView addSubview:label];
        }
        
    }
//    [self.view bringSubviewToFront:_backgroundView];
    return titleView;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *isOpen = [_isOpen objectAtIndex:indexPath.section];

    if ([isOpen isEqualToString:@"YES"]) {
        return 50;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _isOpen.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *isOpen = [_isOpen objectAtIndex:section];
    if (section == 0 && [isOpen isEqualToString:@"YES"]) {
        return [MusicPlayer1 shareInstance].collectList.count;
    }
//    else if (section == 1 && [isOpen isEqualToString:@"YES"]) {
//        return [MusicPlayer1 shareInstance].downloadList.count;
//    }
    else if ([isOpen isEqualToString:@"YES"]) {
        NSArray *array = [[MusicPlayer1 shareInstance].mainList objectAtIndex:section - 1];
        return array.count;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[CDSideBarController sharedInstance] dismissMenu];
    [MusicPlayer1 shareInstance].isPlay = YES;
    [[HearderView shareInstance].startButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    [HearderView shareInstance].currentList = indexPath.section;
    [MusicPlayer1 shareInstance].firstPlay = YES;
    if (indexPath.section == 0) {
        [MusicPlayer1 shareInstance].isDownLoad = NO;
        [MusicPlayer1 shareInstance].isDownLoadList = NO;
        MusicModel *model = [[MusicPlayer1 shareInstance].collectList objectAtIndex:indexPath.row];
        [[MusicPlayer1 shareInstance].list removeAllObjects];
        [[MusicPlayer1 shareInstance].list addObjectsFromArray:[MusicPlayer1 shareInstance].collectList];
        [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL URLWithString:model.url]];
        [MusicPlayer1 shareInstance].currentMusic = indexPath.row;
        [[MusicPlayer1 shareInstance] play];
        [[HearderView shareInstance].listTableView reloadData];
    }
//    else if (indexPath.section == 1) {
//        [MusicPlayer1 shareInstance].isDownLoadList = YES;
//        MusicModel *model = [[MusicPlayer1 shareInstance].downloadList objectAtIndex:indexPath.row];
//        [[MusicPlayer1 shareInstance].list removeAllObjects];
//        [[MusicPlayer1 shareInstance].list addObjectsFromArray:[MusicPlayer1 shareInstance].downloadList];
//        
//        NSString *homePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//        NSString *url = [homePath stringByAppendingPathComponent:model.url];
//       [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL fileURLWithPath:url]];
//        [MusicPlayer1 shareInstance].isDownLoad = YES;
//        [MusicPlayer1 shareInstance].currentMusic = indexPath.row;
//        [[MusicPlayer1 shareInstance] play];
//        [[HearderView shareInstance].listTableView reloadData];
//    }
    else {
        [MusicPlayer1 shareInstance].isDownLoad = NO;
        [MusicPlayer1 shareInstance].isDownLoadList = NO;
        NSArray *array = [[MusicPlayer1 shareInstance].mainList objectAtIndex:indexPath.section - 1];
        MusicModel *model = [array objectAtIndex:indexPath.row];
        [[MusicPlayer1 shareInstance].list removeAllObjects];
        [[MusicPlayer1 shareInstance].list addObjectsFromArray:array];
        [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL URLWithString:model.url]];
        [MusicPlayer1 shareInstance].currentMusic = indexPath.row;
        [[MusicPlayer1 shareInstance] play];
        [[HearderView shareInstance].listTableView reloadData];
    }
}

#pragma -mark delete download
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        MusicModel *model = nil;
        if (indexPath.section == 0) {
//            if (indexPath.row != [MusicPlayer1 shareInstance].currentMusic) {
                if (indexPath.row < [MusicPlayer1 shareInstance].currentMusic) {
                    [MusicPlayer1 shareInstance].currentMusic--;
                }
            model = [[MusicPlayer1 shareInstance].collectList objectAtIndex:indexPath.row];
            [Database deleteValue:model.songId];
            [[MusicPlayer1 shareInstance].collectList removeObjectAtIndex:indexPath.row];
            [[MusicPlayer1 shareInstance].list removeAllObjects];
            [[MusicPlayer1 shareInstance].list addObjectsFromArray:[MusicPlayer1 shareInstance].collectList];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            [tableView reloadData];
            [[HearderView shareInstance].listTableView reloadData];
            //            }
            //            else {
            //                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"歌曲正在播放" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            //                [alert show];
            //            }
            
        }
//        else if (indexPath.section == 1) {
//            if (indexPath.row != [MusicPlayer1 shareInstance].currentMusic ||  [HearderView shareInstance].currentList != 1) {
//                if (indexPath.row < [MusicPlayer1 shareInstance].currentMusic && [HearderView shareInstance].currentList == 1) {
//                    [MusicPlayer1 shareInstance].currentMusic--;
//                }
//                model = [[MusicPlayer1 shareInstance].downloadList objectAtIndex:indexPath.row];
//                [Database deleteDownloadValue:model.songId];
//                [[MusicPlayer1 shareInstance].downloadList removeObjectAtIndex:indexPath.row];
//                NSString *homePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//                NSString *path = [homePath stringByAppendingPathComponent:model.url];
//                [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
//                [[MusicPlayer1 shareInstance].list removeAllObjects];
//                [[MusicPlayer1 shareInstance].list addObjectsFromArray:[MusicPlayer1 shareInstance].downloadList];
//                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//                [tableView reloadData];
//                [[HearderView shareInstance].listTableView reloadData];
//            }
//            else {
//                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"歌曲正在播放" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//                [alert show];
//            }
//        }
        else {
//            if (indexPath.section == _currentSection) {
            //  && indexPath.row != [MusicPlayer1 shareInstance].currentMusic
            if (indexPath.row < [MusicPlayer1 shareInstance].currentMusic) {
                [MusicPlayer1 shareInstance].currentMusic--;
            }
            NSMutableArray *array = [[MusicPlayer1 shareInstance].mainList objectAtIndex:indexPath.section - 1];
            [array removeObjectAtIndex:indexPath.row];
            [[MusicPlayer1 shareInstance].mainList replaceObjectAtIndex:indexPath.section - 1 withObject:array];
            NSArray *tableArray = [Database selectTableName];
            NameModel *nameModel = [tableArray objectAtIndex:indexPath.section - 1];
            NSArray *dataArray = [Database selectData:nameModel.tableName];
            MusicModel *model = [dataArray objectAtIndex:indexPath.row];
            [Database deleteValue:model.songId :nameModel.tableName];
            [[MusicPlayer1 shareInstance].list removeAllObjects];
            [[MusicPlayer1 shareInstance].list addObjectsFromArray:array];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            [tableView reloadData];
            [[HearderView shareInstance].listTableView reloadData];
            //            }
            //            else {
            //                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"歌曲正在播放" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            //                [alert show];
            //            }
        }
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
